﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Core;
using ZDSoft.LMS.Service;
using NHibernate.Criterion;
using ZDSoft.LMS.Web.Apps;

namespace ZDSoft.LMS.Web.Controllers
{
    /// <summary>
    /// 回车报销
    /// </summary>
    public class BillingController : UcController
    {
        /// <summary>
        /// 分页展示user信息,默认为第一页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="userName">查询条件，用户名</param>
        public ActionResult Index(int pageIndex = 1, string userName = "")
        {
            int count = 0;//用于存放满足条件的记录总数
            IList<ICriterion> listQuery = GetCondition(userName);//获取查询条件
            IList<Order> listOrder = new List<Order>() { new Order("ID", true) };//设置一个排序集合，new Order("ID", true)表示：根据ID排序,true代表升序,false代表降序
            IList<DeliveryForm> list = Container.Instance.Resolve<IDeliveryFormService>().GetPaged(listQuery, listOrder, pageIndex, PagerHelper.PageSize, out count);

            PageList<DeliveryForm> pageList = list.ToPageList<DeliveryForm>(pageIndex, PagerHelper.PageSize, count);//转换为PageList集合，用于分页控件显示不同的页码
            return View(pageList);//用pageList集合填充页面。
        }

        private IList<ICriterion> GetCondition(string userName = "")//生成查询条件,userName变量默认为空
        {
            List<ICriterion> queryConditions = new List<ICriterion>();//实例化一个查询条件集合

            if (!string.IsNullOrEmpty(userName))//如果userName变量不为空
            {
                queryConditions.Add(new LikeExpression("UserName", userName));//在查询条件集合中加入一个模糊查询条件
            }
            return queryConditions;//返回查询条件集合
        }

        public ActionResult Create(int id = 0)
        {
            ViewBag.BillingItemTypes = 
                Container.Instance.Resolve<IBillingItemTypeService>().GetAll()
                .OrderBy(o=>o.Name).ToList();
            DeliveryForm form = Container.Instance.Resolve<IDeliveryFormService>().Get(id);
            return View(form);
        }

        [HttpPost]
        public ActionResult Create(string txtValues)
        {
            return RedirectToAction("Index");
        }
    }
}
